<template>
  <el-container class="home-container">
    <!-- 头部 -->
    <el-header>
      <div>
        <!-- <img src="../assets/heima.png" alt=""> -->
        <!-- 在template以及style使用@符号建议加~做兼容 -->
        <img src="~@/assets/heima.png" alt="" />
        <span>电商后台管理系统</span>
      </div>
      <el-button type="success" @click="logout">退出</el-button>
    </el-header>
    <el-container>
      <!-- 左侧 -->
      <el-aside :width="isCollapse ? '64px' : '200px'">
        <div class="toggle-button" @click="toggleCollapse">| | |</div>
        <!-- 颜色 -->
        <el-menu
          unique-opened
          background-color="#333744"
          text-color="#fff"
          active-text-color="#409EFF"
          :collapse="isCollapse"
          :collapse-transition="false"
          router
          :default-active = "activeIndex"
        >
          <el-submenu
            :index="item.id + ''"
            v-for="item in menuList"
            :key="item.id"
          >
            <template slot="title">
              <i :class="iconsObj[item.id]"></i>
              <span>{{ item.authName }}</span>
            </template>
            <!-- 二级 -->
            <el-menu-item
              :index="'/' + subItem.path"
              v-for="subItem in item.children"
              :key="subItem.id"
              @click="saveNavState('/' + subItem.path)"
            >
              <template slot="title">
                <i class="el-icon-menu"></i>
                <span>{{ subItem.authName }}</span>
              </template>
            </el-menu-item>
          </el-submenu>
        </el-menu>
      </el-aside>
      <!-- 主内容 -->
      <el-main>
        <router-view></router-view>
      </el-main>
    </el-container>
  </el-container>
</template>

<script>
export default {
  name: 'Home',
  data () {
    return {
      menuList: [],
      iconsObj: {
        125: 'el-icon-user-solid',
        103: 'el-icon-s-grid',
        101: 'el-icon-shopping-cart-2',
        102: 'el-icon-s-order',
        145: 'el-icon-s-platform'
      },
      // 是否折叠
      isCollapse: false,
      activeIndex: ''

    }
  },
  created () {
    this.activeIndex = window.sessionStorage.getItem('activeIndex')
    this.getMenus()
  },
  methods: {
    // 保存路径
    saveNavState (activeIndex) {
      window.sessionStorage.setItem('activeIndex', activeIndex)
      this.activeIndex = activeIndex
    },
    // 折叠菜单
    toggleCollapse () {
      this.isCollapse = !this.isCollapse
    },
    // 获取菜单
    async getMenus () {
      const { data: res } = await this.$http.get('menus')
      if (res.meta.status !== 200) {
        return this.$message.error(res.meta.msg)
      }
      this.menuList = res.data
    },
    // 退出
    logout () {
      window.sessionStorage.removeItem('token')
      this.$router.push('./login')
    }
  }
}
</script>

<style lang="less" scoped>
.home-container {
  height: 100%;
  .el-header {
    background-color: #373d41;
    display: flex;
    justify-content: space-between;
    padding-left: 0;
    align-items: center;
    color: #fff;
    font-size: 20px;
    div {
      display: flex;
      align-items: center;
    }
    span {
      margin-left: 15px;
    }
  }
  .el-aside {
    background-color: #333744;

    .toggle-button {
    background-color: #4a5064;
    font-size: 10px;
    line-height: 24px;
    color: #fff;
    text-align: center;
    }
    .iconfont {
      margin-right: 10px;
    }

    .el-menu {
      border-right: 1px;
    }
  }
  .el-main {
    background-color: #eaedf1;
  }

}
</style>
